/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "hilog/log.h"
#include "preferred_language.h"
#include "locale_config.h"
#include "i18n_ffi.h"
 
namespace OHOS {
namespace Global {
namespace I18n {
using namespace OHOS::HiviewDFX;
extern "C"
{
char* MallocCString(const std::string& origin)
{
    if (origin.empty()) {
        return nullptr;
    }
    auto length = origin.length() + 1;
    char* res = static_cast<char*>(malloc(sizeof(char) * length));
    if (res == nullptr) {
        return nullptr;
    }
    return std::char_traits<char>::copy(res, origin.c_str(), length);
}

char* FfiOHOSGetAppPreferredLanguage()
{
    std::string language = PreferredLanguage::GetFirstPreferredLanguage();
    char* res = MallocCString(language);
    return res;
}

char* FfiI18nSystemGetSystemLanguage()
{
    std::string language = OHOS::Global::I18n::LocaleConfig::GetSystemLanguage();
    return MallocCString(language);
}
char* FfiI18nSystemGetSystemRegion()
{
    std::string region = OHOS::Global::I18n::LocaleConfig::GetSystemRegion();
    return MallocCString(region);
}
}
}
}
}